﻿@page "/channel"
@using AdminShared.Models.Article

<Layout Style=" padding: 0 24px 24px;">
    <Breadcrumb Style="margin: 16px 0;">
        <BreadcrumbItem>返回上一页</BreadcrumbItem>
        <BreadcrumbItem>首页</BreadcrumbItem>
        <BreadcrumbItem>频道管理</BreadcrumbItem>
        <BreadcrumbItem>所有频道</BreadcrumbItem>
    </Breadcrumb>
    <Content Class="site-layout-background">

        <div style="margin-bottom:10px">
            <Button Icon="plus" Type="@ButtonType.Primary" @onclick="()=>EditChannel()">添加</Button>
        </div>
        <Table TItem="DtoChannel" DataSource="@pageList.List" Total="@pageList.Total" Loading="tableLoading" RemoteDataSource>

            <RowTemplate>
                <Column @bind-Field="@context.Name" Title="名称"></Column>
                <Column @bind-Field="@context.Remarks" Title="备注"></Column>
                <Column @bind-Field="@context.Sort" Title="排序"></Column>
                <Column Field="@context.CreateTime.LocalDateTime" Format="yyyy-MM-dd HH:mm" Title="创建时间">
                </Column>
                <ActionColumn Title="操作">
                    <Space>
                        <SpaceItem>
                            <a @onclick="()=>EditChannel(context)">编辑</a>
                        </SpaceItem>
                        <SpaceItem>
                            <Popconfirm Title="确认要删除吗？"
                                        OnConfirm="_=>DeleteChannel(context.Id)"
                                        OkText="Yes"
                                        CancelText="No">
                                <a style="color:red">删除</a>
                            </Popconfirm>

                        </SpaceItem>
                    </Space>
                </ActionColumn>
            </RowTemplate>

            <PaginationTemplate>
                <div style="margin:15px 0;float:right">
                    <Pagination Total="@pageList.Total" PageSize="pageSize" Current="pageNum" ShowSizeChanger OnChange="PageChange" ShowQuickJumper ShowTotal="showTotal" />
                </div>
            </PaginationTemplate>

        </Table>



    </Content>
</Layout>


<Drawer Closable="true" Width="380" Visible="isShowEditChannel" Title='("频道信息")' OnClose="()=>isShowEditChannel=false">
    <Template style="height:90%">

        <Form Model="@editChannel" OnFinish="SaveChannel">

            <FormItem>
                <Text>名称</Text>
                <Input Placeholder="请输入名称" @bind-Value="@context.Name" />
            </FormItem>

            <FormItem>
                <Text>备注</Text>
                <Input Placeholder="请输入昵称" @bind-Value="@context.Remarks" />
            </FormItem>


            <FormItem>
                <Text>排序</Text>
                <Input Placeholder="请输入排序值" Type="number" @bind-Value="@context.Sort" />
            </FormItem>


            <Row Gutter="24">
                <AntDesign.Col Span="24">
                    <div style="float:right">
                        <Button Type="@ButtonType.Primary" HtmlType="submit" Loading="@saveLoading">保存</Button>
                        <Button Type="@ButtonType.Default" @onclick="()=>isShowEditChannel=false">取消</Button>
                    </div>
                </AntDesign.Col>
            </Row>
        </Form>
    </Template>
</Drawer>


@code {

    override protected void OnInitialized()
    {
        GetChannelList();
    }

    bool saveLoading = false;
    bool tableLoading = false;
    int pageNum = 1;
    int pageSize = 10;
    DtoPageList<DtoChannel> pageList = new();
    async void GetChannelList()
    {
        tableLoading = true;
        var retData = await Http.GetFromJsonAsync<DtoPageList<DtoChannel>>("Article/GetChannelList?pageNum=" + pageNum + "&pageSize=" + pageSize);

        if (retData != null)
        {
            pageList = retData;
        }

        tableLoading = false;
        StateHasChanged();
    }
    void PageChange(PaginationEventArgs args)
    {
        if (pageNum != args.Page)
        {
            pageNum = args.Page;
        }

        if (pageSize != args.PageSize)
        {
            pageSize = args.PageSize;
        }

        GetChannelList();
    }
    Func<PaginationTotalContext, string> showTotal = pageList => $"共 {pageList.Total} 条";


    bool isShowEditChannel = false;
    DtoEditChannel editChannel = new();
    long channelId;

    void EditChannel(DtoChannel? channel = null)
    {
        editChannel = new();
        channelId = default;

        if (channel != null)
        {
            channelId = channel.Id;
            editChannel.Name = channel.Name;
            editChannel.Remarks = channel.Remarks;
            editChannel.Sort = channel.Sort;
        }
        else
        {
            editChannel.Sort = 99;
        }

        isShowEditChannel = true;

        StateHasChanged();
    }


    async void SaveChannel()
    {
        saveLoading = true;
        if (channelId == default)
        {
            using (var httpResponse = await Http.PostAsJsonAsync<DtoEditChannel>("Article/CreateChannel", editChannel))
            {
                var createChannelId = httpResponse.Content.ReadAsStringAsync().Result;

                Message.Success("添加成功");
            }
        }
        else
        {
            using (var httpResponse = await Http.PostAsJsonAsync<DtoEditChannel>("Article/UpdateChannel?channelId=" + channelId, editChannel))
            {
                var updateChannelRet = httpResponse.Content.ReadAsStringAsync().Result;

                if (bool.Parse(updateChannelRet))
                {
                    Message.Success("编辑成功");
                }
            }
        }

        saveLoading = false;

        GetChannelList();

        isShowEditChannel = false;
    }


    async void DeleteChannel(long channelId)
    {
        using (var httpResponse = await Http.DeleteAsync("Article/DeleteChannel?id=" + channelId))
        {
            var retValue = httpResponse.Content.ReadAsStringAsync().Result;

            if (Convert.ToBoolean(retValue))
            {
                GetChannelList();
                Message.Success("删除成功");
            }
        }
    }

}
